package com.eatme.web;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Coordinate;
import net.coobird.thumbnailator.geometry.Position;
import net.coobird.thumbnailator.geometry.Positions;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class ImageHandle {


    public static void main(String[] args) throws IOException {

        String imgPath = "C:\\Users\\dongkang\\Desktop\\orgin.bmp";

        String target = "C:/Users/dongkang/Desktop/target.bmp";

        File srcFile = new File(imgPath);

        BufferedImage image = ImageIO.read(srcFile);

        int width = image.getWidth();

        int height = image.getHeight();

        //原图大小为：800，480

        System.out.println("原图宽度为：" + width + " ，高度为 " + height);

        //此功能由于项目中使用了此种方式，故代码比较多，功能相对比较多，但具体的代码，参数应该自行设置

        //水印可以同时添加多个，添加方式为：.watermark(顶部).watermark(中部).watermark(底部)

        watermark(srcFile, new File(srcFile.getParent() , "target.png"),
                Positions.TOP_LEFT , "旺城大道37号咪表-03车位(038373)","2元","2022-02-11 16:24:42","2022-02-11 16:24:42","0小时15分钟1秒", 01f);
        //给较长图添加一屏一个水印


    }










    /**

     * 添加文本水印，也可添加图片水印

     */

    public static void watermark(File srcFile , File outFile , Position position , String roadInfo,String fee,String enterTime,String nowTime,String duration,float opacity){
        //从原图中找出300x300的大小来显示水印文本

        BufferedImage bi = new BufferedImage(800, 480 ,BufferedImage.TYPE_INT_RGB);

        Graphics2D g = bi.createGraphics();

        //设置绘图区域透明

        bi = g.getDeviceConfiguration().createCompatibleImage(800, 480,Transparency.TRANSLUCENT);

        g.dispose();

        //字体、字体大小，透明度，旋转角度

        g = bi.createGraphics();

        g.setFont(new Font("微软雅黑", Font.PLAIN, 26));

        char[] data = roadInfo.toCharArray();
        g.setColor(Color.white);

        //设置文本显示坐标，以上述中的300x300位置为0,0点

        g.drawChars(data, 0, data.length, 70, 88);
        char[] data2 = fee.toCharArray();
        g.drawChars(data2, 0, data2.length, 450, 248);

        char[] data3 = enterTime.toCharArray();
        g.drawChars(data3, 0, data3.length, 450, 282);

        char[] data4 = nowTime.toCharArray();
        g.drawChars(data4, 0, data4.length, 450, 320);

        char[] data5 = duration.toCharArray();
        g.drawChars(data5, 0, data5.length, 450, 360);
        try {
            Thumbnails.of(srcFile).scale(1).watermark(position, bi, opacity).outputQuality(0.20f).toFile(outFile);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
